<?php
namespace App\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display();
    }
    
    public function getwxurl(){
        $WCHART_CONFIG=C('WCHART_CONFIG');
        $redirect_url=urlencode($WCHART_CONFIG['REDIRECT_URI']);
        $state=get_unique_no();
        session('state',$state);
        $redirect_url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='. $WCHART_CONFIG['APPID'] . '&' .'redirect_uri=' . $redirect_url . '&response_type=code&scope=snsapi_userinfo&state='. $state .'#wechat_redirect';
        
        echo '<script>window.location.replace("'. $redirect_url .'")</script>';
    }

    public function redirecta(){
        $this->display();
    }

    // 签到
    public function register(){
        $WCHART_CONFIG=C('WCHART_CONFIG');

        $code=$_POST['code'];
        $state=$_POST['state'];


        if($state!=session('state')){
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'标识不一致'));
            return ;
        }



        if($code && $state){

            // 请求access_token openid
            $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='. $WCHART_CONFIG['APPID'] .'&secret='. $WCHART_CONFIG['SERECT'] .'&code='. $code .'&grant_type=authorization_code';
            $get_openid_res=doCurlGetRequest($url);

            if(!$get_openid_res['openid']){
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'请求秘钥失败'));
                return ;
            }


            // 请求用户信息
            $get_userinfo_url='https://api.weixin.qq.com/sns/userinfo?access_token='. $get_openid_res['access_token'] .'&openid='. $get_openid_res['openid'] .'&lang=zh_CN';
            $get_userInfo_res=doCurlGetRequest($get_userinfo_url);


            if(!$get_userInfo_res['openid']){
                $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'请求用户信息失败'));
                return ;
            }


            // 检测用户是否关注微信公众号
            $access_token=get_buse_token();
            $check_user_to_weichart='https://api.weixin.qq.com/cgi-bin/user/info?access_token=' . $access_token . '&openid=' . $get_userInfo_res['openid'] . '&lang=zh_CN';
            $get_user_to_weichart_res=doCurlGetRequest($check_user_to_weichart);

            if($get_user_to_weichart_res['subscribe']==0){
                $this->ajaxReturn(array('return_code'=>2,'return_msg'=>'没有关注微信号'));
                return ;
            }
            

            $user=M('user');

            $select_map=array(
                'user_openid'=>$get_userInfo_res['openid']
            );

            $user_select_res=$user->where($select_map)->select();

            if($user_select_res[0]){

                session('openid',$get_userInfo_res['openid']);

                $save_data=array(
                    'user_openid'=>$get_userInfo_res['openid'],
                    'user_nickname'=>$get_userInfo_res['nickname'],
                    'user_sex'=>$get_userInfo_res['sex'],
                    'user_province'=>$get_userInfo_res['province'],
                    'user_city'=>$get_userInfo_res['city'],
                    'user_country'=>$get_userInfo_res['country'],
                    'user_headimgurl'=>$get_userInfo_res['headimgurl'],
                    'user_privilege'=>$get_userInfo_res['privilege'],
                    'user_unionid'=>$get_userInfo_res['unionid'] ? $get_userInfo_res['unionid'] : '没有关注公众号'
                );
                
                $user->where($select_map)->save($save_data);

                $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'签到成功','return_body'=>$get_userInfo_res));

            }else{
                
                $add_data=array(
                    'user_openid'=>$get_userInfo_res['openid'],
                    'user_nickname'=>$get_userInfo_res['nickname'],
                    'user_sex'=>$get_userInfo_res['sex'],
                    'user_province'=>$get_userInfo_res['province'],
                    'user_city'=>$get_userInfo_res['city'],
                    'user_country'=>$get_userInfo_res['country'],
                    'user_headimgurl'=>$get_userInfo_res['headimgurl'],
                    'user_privilege'=>$get_userInfo_res['privilege'],
                    'user_unionid'=>$get_userInfo_res['unionid'] ? $get_userInfo_res['unionid'] : '没有关注公众号'
                );

                // 推送信息
                push_message('check_user_login',$add_data);

                $user_add_res=$user->data($add_data)->add();


                if($user_add_res){
                    $presign=M('presign');
                    $customer=M('customer');

                    $presign_select_res=$presign->where(array('user_openid'=>$get_userInfo_res['openid']))->select();
                    if($presign_select_res){
                        $user_add_res=$customer->data($add_data)->add();
                    }
                    
                }

                if($user_add_res){
                    $this->ajaxReturn(array('return_code'=>1,'return_msg'=>'签到成功','return_body'=>$get_userInfo_res));              
                }else{
                    $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'存表失败'));
                }
            }
        }else{
            $this->ajaxReturn(array('return_code'=>0,'return_msg'=>'参数为空'));
        }

    }
}
